package application

import (
	"cloud_android_backend/pkg/models/role"
	"github.com/jinzhu/gorm"
	"time"
)

type Application struct {
	Id          int       `json:"id" gorm:"column:id;primary_key"`
	AppName     string    `json:"app_name" gorm:"column:app_name"`
	WelcomeMsg  string    `json:"welcome_msg" gorm:"column:welcome_msg"`
	ModelId     int       `json:"model_id" gorm:"column:model_id"`
	RoleSet     string    `json:"role_set" gorm:"column:role_set"`
	MultiConver int       `json:"multi_conver" gorm:"column:multi_conver"`
	RoleId      string    `json:"role_id" gorm:"column:role_id"`
	VoiceId     int       `json:"voice_id" gorm:"column:voice_id"`
	UserId      string    `json:"user_id" gorm:"column:user_id"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at"`
	Status      int       `json:"status" gorm:"column:status"`
	Role        role.Role `json:"role" gorm:"foreignKey:RoleId"`
	BotId       string    `json:"bot_id" gorm:"column:bot_id"`
}

func (a *Application) TableName() string {
	return "application"
}
func (a *Application) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *Application) Update(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Updates(a).Error
}
func (a *Application) GetById(db *gorm.DB) error {
	return db.Table(a.TableName()).Preload("Role").Where("id = ?", a.Id).First(a).Error
}
func (a *Application) GetListByUserId(db *gorm.DB) (applications []Application, err error) {
	return applications, db.Table(a.TableName()).Preload("Role").Where("user_id = ?", a.UserId).Where("status = ?", 1).Find(&applications).Error
}
func (a *Application) Delete(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Delete(a).Error
}
